<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
            <el-form-item label="活动时间" required>
                <el-col :span="6">
                <el-form-item prop="date1">
                    <el-date-picker
                    v-model="ruleForm.date1"
                    type="datetime"
                    placeholder="选择开始时间">
                    </el-date-picker>
                </el-form-item>
                </el-col>
                <el-col class="line" :span="2">-</el-col>
                <el-col :span="8">
                <el-form-item prop="date2">
                    <el-date-picker
                    v-model="ruleForm.date2"
                    type="datetime"
                    placeholder="选择结束时间">
                    </el-date-picker>
                </el-form-item>
                </el-col>
            </el-form-item>
            <el-form-item label="满足金额" prop="least">
                <el-input v-model="ruleForm.least" style="width:300px"></el-input>
            </el-form-item>
            <el-form-item label="可抵消金额" prop="offset">
                <el-input v-model="ruleForm.offset" style="width:300px"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">立即提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    name:"coupon",
    data() {
      return {
        ruleForm: {
          least: '',
          offset: '',
          date1: '',
          date2: ''
        },
        rules: {
          least: [
            { required: true, message: '请输入满足金额', trigger: 'blur' },
            { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
          ],
          offset: [
            { required: true, message: '请输入可抵消金额', trigger: 'blur' },
            { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
          ],
          date1: [
            { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
          ],
          date2: [
            { type: 'date', required: true, message: '请选择时间', trigger: 'change' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.ruleForm.date1 = this.$moment(this.ruleForm.date1).format("YYYY-MM-DD HH:mm:ss");
            this.ruleForm.date2 = this.$moment(this.ruleForm.date2).format("YYYY-MM-DD HH:mm:ss");
            this.$axios.post("/api/insertCoupon",this.$querystring.stringify({
            belong:this.$cookies.get("shopname"),
            start:this.ruleForm.date1,
            end:this.ruleForm.date2,
            least:this.ruleForm.least,
            offset:this.ruleForm.offset,
            })).then(reponse=>{
            console.log(reponse);
            if(reponse.data == true){
                /*
                成功跳转
                */
               alert("提交成功")
            }else{
                alert("提交失败")
            }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
}
</script>